<template>
  <base-breadcrumb
    v-loading="loading"
    :title="title"
    :breadcrumb="!isAudit"
  >
    <div class="about-master-container">
      <a-tabs
        type="card"
        :active-key="activeKey"
        @change="callback"
      >
        <a-tab-pane
          key="1"
          tab="基本信息"
        >
          <BaseInfo
            v-if="!baseChangeStatus"
            :base-form="baseForm"
            :type="type"
            :select-option-list="selectOptionList"
            @update="getData"
          />
          <change
            v-else
            :state="state"
          />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 2"
          key="2"
          tab="合同评审（分公司）"
        >
          <ContractReviewBranch
            ref="ContractReviewBranch"
            :state="state"
            type="Branch"
            :base-form="baseForm"
          />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 2"
          key="10"
          tab="合同评审（公司）"
        >
          <ContractReviewBranch
            ref="ContractReviewBranch"
            :state="state"
            type="company"
            :base-form="baseForm"
          />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 3"
          key="3"
          tab="合同评审汇总（公司）"
        >
          <ContractReview
            :select-option-list="selectOptionList"
            :base-form="baseForm"
          />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 4"
          key="4"
          tab="授权申请"
        >
          <AuthApply
            :select-option-list="selectOptionList"
            :base-form="baseForm"
          />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 5"
          key="5"
          tab="用印申请"
        >
          <UsePrintApply :base-form="baseForm" />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 6"
          key="6"
          tab="成本分析表"
        >
          <CostAnalysisTable :base-form="baseForm" />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 7"
          key="7"
          tab="资金策划表"
        >
          <MoneyPlanning :base-form="baseForm" />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 8"
          key="8"
          tab="重大风险化解措施"
        >
          <LargeRisk :base-form="baseForm" />
        </a-tab-pane>
        <a-tab-pane
          v-if="type === 9"
          key="9"
          tab="风险复核"
        >
          <DangerReview :base-form="baseForm" />
        </a-tab-pane>
        <!-- <a-tab-pane
          v-if="baseForm.baseId"
          key="10"
          tab="相关附件"
        >
          <div>
            <a-card>
              <title-name title="相关" />
              <u-table
                business-code="LCM08"
                :list-url="findFileList"
                :upload-list="addFile"
                :delete-url="deleteFile"
                :business-id="baseForm.baseId"
              />
            </a-card>
            <div
              class="operation"
              style="text-align: right; margin-right: 20px"
            >
              <a-button @click="$close()">
                关闭
              </a-button>
            </div>
          </div>
        </a-tab-pane> -->
      </a-tabs>
    </div>
  </base-breadcrumb>
</template>
<script>
import ContractReviewBranch from './components/ContractReviewBranch' //tab2  合同评审（分公司）
import AuthApply from './components/AuthApply'
import UsePrintApply from './components/UsePrintApply'
import CostAnalysisTable from './components/CostAnalysisTable'
import ContractReview from './components/ContractReview'
import MoneyPlanning from './components/MoneyPlanning'
import LargeRisk from './components/LargeRisk'
import BaseInfo from './components/BaseInfo'
import DangerReview from './components/DangerReview'
import change from './components/change'
//api接口
import totalPackageContractApi from '@/api/legal/branchContract/totalPackageContract'

export default {
    name:'a'+Date.now(),
    components: {
        ContractReviewBranch,
        AuthApply,
        UsePrintApply,
        CostAnalysisTable,
        ContractReview,
        MoneyPlanning,
        LargeRisk,
        BaseInfo,
        DangerReview,
        change
    },
    props: {
        isAudit: {
            type: Boolean,
            default: false
        }
    },
    data() {
        return {
            loading: false,
            state: Number(this.$route.query.state),
            baseChangeStatus: this.$route.query.baseChangeStatus == '1',
            type: null,
            activeKey: '1',
            baseForm: {
                branchCompanyId: '',
                projectId: '',
                otherParty: ''
            },
            addFile: totalPackageContractApi.addFile(),
            deleteFile: totalPackageContractApi.deleteFile(),
            findFileList: totalPackageContractApi.findFileList(),
            selectOptionList: {}
        }
    },
    computed: {
        title() {
          let status = + this.state == 1 ? '新增' : this.state == 2 ? '编辑':  this.state == 3 ?'查看' : ''
            switch (this.type) {
                case 1:
                    return '总包合同管理编辑' + status
                case 2:
                    return '合同评审（分公司）' + status
                case 3:
                    return '合同评审（公司）' + status
                case 4:
                    return '授权申请' + status
                case 5:
                    return '用印申请' + status
                case 6:
                    return '成本分析表' + status
                case 7:
                    return '资金策划表' + status
                case 8:
                    return '重大风险化解措施' + status
                case 9:
                    return '风险复核' + status
                case 10:
                    return '总包合同变更' + status
            }
            return '总包合同管理'  + status
        }
    },
    watch: {
        isAudit: {
            handler(val, oldVal) {
                if (val) {
                    setTimeout(() => {
                        this.type = 2
                        this.state = 2
                        this.activeKey = this.type + ''
                    }, 100)
                }
            },
            immediate: true
        }
    },
    created: async function () {
      //合同变更时不请求下面
      if(this.baseChangeStatus){
        if (this.$route.query.type) {
          this.type = Number(this.$route.query.type)
        }
          return false
      }
        if (this.$route.query.id) {
            this.baseForm.baseId = this.$route.query.id
            await this.getData()
        }
        if (this.$route.query.type) {
            this.type = Number(this.$route.query.type)
            this.activeKey = this.type + ''
        }
        this.getArr('contract', 12)
        this.getArr('auth', 7)
        this.getArr('companyContractType', 13)
        this.getArr('companyContractLevel', 14)
        this.getArr('projectFormats', 18)
        this.getArr('projectTypes', 19)
        this.getArr('settleCnditionss', 20)
    },
    mounted() {
        this.$bus.$on('submit', this.submit)
    },
    destroyed() {
        this.$bus.$off('submit')
    },
    methods: {
        async getData(id) {
            this.loading = true
            let params = { baseId: id || this.baseForm.baseId, edit: true }
            if (this.$route.query.type) {
                params.edit = false
            }
            let res = await totalPackageContractApi.findListByMainId({}, params)
            if (res.code === 200) {
                this.baseForm = res.data
            }
            this.loading = false
        },
        async getArr(key, type) {
            let res = await totalPackageContractApi.typeslabel({
                pageNo: 0,
                pageSize: 100000,
                query: { type }
            })
            if (res.code === 200) {
                this.$set(this.selectOptionList, key, res.data)
            }
        },
        async submit(code, isMajorRisks) {
            let that = this
            this.$confirm({
                title: this.SUBMIT_CONFIRM_MSG,
                okText: '是',
                okType: 'danger',
                cancelText: '否',
                onOk: async () => {
                    that.loading = true
                    let res = await totalPackageContractApi.generalSubmit({
                        auditStatusEnum: 'SUBMITTED',
                        baseId: that.baseForm.baseId,
                        majorRisks: isMajorRisks,
                        submitCode: code
                    })
                    if (res.code === 200) {
                        that.$noticeSuccess('提交成功')
                        that.$bus.$emit('submited', true)
                        setTimeout(that.$multiTab.close, that.TIMEOUT_DELAY)
                    } else {
                        that.$bus.$emit('submited', false)
                    }
                    that.loading = false
                },
                onCancel() {
                    that.$bus.$emit('submited', false)
                }
            })
        },
        callback(key) {
            this.activeKey = key
        }
    }
}
</script>
<style lang="less">
#components-layout-demo-custom-trigger .ant-tabs-bar.ant-tabs-top-bar.ant-tabs-card-bar {
    margin: 0 !important;
}

.about-master-container {
    padding: 0 15px;

    .ant-card-body {
        padding: 20px;

        .tab6 {
            tbody {
                text-align: center;
            }
        }
    }
}
</style>
